package com.haierp.controller.authority;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.haierp.model.authority.ResourceHai;
import com.haierp.service.authority.IResourceServiceHai;
import com.haierp.util.JsonResult;
import com.haierp.vo.authority.ResourceQueryVO;

/**
 * 
 * Title: ResourceController.java
 * Description: May 6, 2017 : Add queryTree
 *
 * @author jc
 * Apr 20, 2017
 *
 */
@Service("resourceControllerHai")
@Controller
@RequestMapping("resourceHai")
public class ResourceControllerHai {
	
	@Autowired
	private IResourceServiceHai iResourceServiceHai;

	@RequestMapping("/add")
	@ResponseBody
	public Object add(ResourceHai resource) {
		JsonResult<String> result = new JsonResult<>();
		
		if (null == resource.getId()) {
			
			if (StringUtils.isBlank(resource.getName())) {
				return result.buildMsg("资源名不能为空").buildIsSuccess(false);
			}
			
			ResourceQueryVO resourceQueryVO = new ResourceQueryVO();
			resourceQueryVO.setPid(resource.getPid());
			resourceQueryVO.setName(resource.getName());
			Integer count = iResourceServiceHai.queryResourcesCount(resourceQueryVO);
			if ((null != count) && (count > 0)) {
				return result.buildMsg("同级资源名不能重复").buildIsSuccess(false);
			}
			
			resource.setCreateTime(new Date());
			iResourceServiceHai.insert(resource);
	        result.buildIsSuccess(true);
		} else {
			result.buildMsg("错误数据").buildIsSuccess(false);
		}
		
		return result;
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public Object update(ResourceHai resource) {
		JsonResult<String> result = new JsonResult<>();
		
		if (null != resource.getId()) {			
			if (StringUtils.isBlank(resource.getName())) {
				return result.buildMsg("资源名不能为空").buildIsSuccess(false);
			}

			ResourceHai temp = new ResourceHai();
			temp.setName(resource.getName());
	    	EntityWrapper<ResourceHai> wrapper = new EntityWrapper<ResourceHai>(temp);
	    	if (null == resource.getPid()) {
	    		wrapper.where("pid is null", resource.getPid());
			} else {
				wrapper.where("pid = {0}", resource.getPid());
			}
            wrapper.where("id != {0}", resource.getId());
	    	Integer count = iResourceServiceHai.selectCount(wrapper);
	    	if ((null != count) && (count > 0)) {
				return result.buildMsg("同级资源名不能重复").buildIsSuccess(false);
			}
			
			resource.setCreateTime(new Date());
			iResourceServiceHai.updateById(resource);
	        result.buildIsSuccess(true);
		} else {
			result.buildMsg("错误数据").buildIsSuccess(false);
		}
		
		return result;
	}

	@RequestMapping("/delete")
	@ResponseBody
	public Object delete(Long id) {
		JsonResult<String> result = new JsonResult<>();

		if (null != id) {
			iResourceServiceHai.deleteResourceById(id);

			result.buildIsSuccess(true);
		} else {
			result.buildMsg("错误数据").buildIsSuccess(false);
		}
		
		return result;
	}
	
	@RequestMapping("/query")
	@ResponseBody
	public Object query(Long id) {
		JsonResult<ResourceHai> result = new JsonResult<>();
		
		return result.buildData(iResourceServiceHai.selectById(id)).buildIsSuccess(true);
	}
	
	/**
	 * 以列表条目的形式返回所有的资源记录
	 * @param resourceQueryVO
	 * @return
	 */
	@RequestMapping("/queryList")
	@ResponseBody
	public Object queryList() {
		JsonResult<List<ResourceHai>> result = new JsonResult<List<ResourceHai>>();
		
		result.setData(iResourceServiceHai.queryResourceList());

		return result.buildIsSuccess(true);
	}
	
	/**
	 * 
	 * @param resourceQueryVO
	 * @return
	 */
	@RequestMapping("/queryTree")
	@ResponseBody
	public Object queryTree() {
		JsonResult<List<ResourceHai>> result = new JsonResult<List<ResourceHai>>();
		
		result.setData(iResourceServiceHai.queryResourceTree(null));

		return result.buildIsSuccess(true);
	}
}
